package com.xiaoshuidi.cloud.module.iot.dal.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.db.TenantBaseDO;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.mapper.TbAccountDoMapper;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbAccountDo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Feng
 * @date 2023/6/16
 */
@Slf4j
@Repository
public class TbAccountRepository extends ServiceImpl<TbAccountDoMapper, TbAccountDo> {


    /**
     * 根据api_no列表查询所有的tb_accounts记录
     *
     * @param apiNos
     * @return
     */
    public List<TbAccountDo> getApiListByApiNos(List<String> apiNos) {
        LambdaQueryWrapper<TbAccountDo> wrapper = Wrappers.lambdaQuery();
        wrapper.in(TbAccountDo::getApiNo, apiNos).eq(TbAccountDo::getStatus, 1);
        List<TbAccountDo> tbAccountDos = list(wrapper);
        return tbAccountDos;
    }

    /**
     * 根据apiNo查询tb_account记录
     *
     * @param apiNo
     * @return
     */
    public TbAccountDo getThirdApiByApiNo(String apiNo) {
        LambdaQueryWrapper<TbAccountDo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbAccountDo::getApiNo, apiNo).eq(TbAccountDo::getStatus, 1);
        TbAccountDo tbAccountDo = this.getOne(wrapper);
        return tbAccountDo;
    }

    /**
     * 根据brandNo查询tb_account记录
     *
     * @param brandNo
     * @return
     */
    public List<TbAccountDo> getApiListByBrandNo(String brandNo) {
        LambdaQueryWrapper<TbAccountDo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbAccountDo::getBrandNo, brandNo).eq(TbAccountDo::getStatus, 1);
        List<TbAccountDo> tbAccountDoList = this.list(wrapper);
        return tbAccountDoList;
    }

    @TenantIgnore
    public TbAccountDo getTbAccountByBnAndLnAndLk(TbAccountDo tbAccountModel) {
        TbAccountDo tbAccountDo = null;
        try {
            LambdaQueryWrapper<TbAccountDo> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(TbAccountDo::getBrandNo, tbAccountModel.getBrandNo())
                    .eq(TbAccountDo::getLoginName, tbAccountModel.getLoginName())
                    .eq(TbAccountDo::getLoginKey, tbAccountModel.getLoginKey())
                    .eq(TbAccountDo::getStatus, 1);
            tbAccountDo = this.getOne(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tbAccountDo;
    }

    public boolean deleteTbAccountRecord(String apiNo) {
        LambdaQueryWrapper<TbAccountDo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbAccountDo::getStatus, 1)
                .eq(TbAccountDo::getApiNo, apiNo);
        TbAccountDo one = this.getOne(wrapper);
        return this.removeById(one);
    }

    public List<String> getApiNosByUserId() {
        LambdaQueryWrapper<TbAccountDo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbAccountDo::getStatus, 1);
        List<TbAccountDo> tbAccountDos = this.list(wrapper);
        List<String> list = new ArrayList<>();
        for (TbAccountDo tbAccountDo : tbAccountDos) {
            String apiNo = tbAccountDo.getApiNo();
            list.add(apiNo);
        }
        return list;
    }

    public ResultModel<PageInfo<TbAccountDo>> getDeviceAccount(Long userId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<TbAccountDo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbAccountDo::getStatus, 1);
        List<TbAccountDo> tbAccountDos = this.list(wrapper);
        PageInfo<TbAccountDo> tbAccountDoPageInfo = new PageInfo<>(tbAccountDos);
        return ResultModel.createSuccess(tbAccountDoPageInfo);
    }

}
